package demo.work;

import com.demo.utils.Brand;
import com.demo.utils.DataSourceUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Test {
    public static void main(String[] args) throws SQLException {
        Brand brand = new Brand(null, "华为手机", "华为公司", 5, "垃圾手机", 1);
        /*int add = add(brand);
        System.out.println(add);*/

        /*int update = update(4);
        System.out.println(update);*/

        int del = delete(4);
        System.out.println(del);

        List<Brand> list = selectAllDate();
        for (Brand brand1 : list) {
            System.out.println(brand1);
        }


    }

    private static int delete(int id) throws SQLException {
        Connection connection = DataSourceUtils.getConnection();
        String sql = "delete from tb_brand where id=?";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setInt(1, id);
        int i = ps.executeUpdate();
        return i;

    }

    private static int update(int id) throws SQLException {
        Connection connection = DataSourceUtils.getConnection();
        String sql = "update tb_brand set ordered=1 where id=?";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setInt(1, id);
        int i = ps.executeUpdate();
        return i;

    }

    private static int add(Brand brand) throws SQLException {
        Connection connection = DataSourceUtils.getConnection();
        String sql = "INSERT INTO tb_brand (id,brand_name, company_name, ordered, description, status) values (null,?,?,?,?,?)";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setString(1, brand.getBrandName());
        ps.setString(2, brand.getCompanyName());
        ps.setInt(3, brand.getOrdered());
        ps.setString(4, brand.getDescription());
        ps.setInt(5, brand.getStatus());
        int i = ps.executeUpdate();
        return i;
    }

    private static List<Brand> selectAllDate() throws SQLException {
        List<Brand> list = new ArrayList<>();
        Connection connection = DataSourceUtils.getConnection();
        String sql = "select * from tb_brand";
        PreparedStatement ps = connection.prepareStatement(sql);
        ResultSet resultSet = ps.executeQuery();
        while (resultSet.next()) {
            int id = resultSet.getInt("id");
            String name = resultSet.getString("brand_name");
            String company_name = resultSet.getString("company_name");
            int ordered = resultSet.getInt("ordered");
            String description = resultSet.getString("description");
            int status = resultSet.getInt("status");

            Brand brand = new Brand(id, name, company_name, ordered, description, status);

            list.add(brand);
        }
        resultSet.close();
        ps.close();
        connection.close();
        return list;
    }
}
